package io.renren.modules.sys.controller;

import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.service.SysUserRoleService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Controller公共组件
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月9日 下午9:42:26
 */
public abstract class AbstractController {

	@Autowired
	private SysUserRoleService sysUserRoleService;

	protected Logger logger = LoggerFactory.getLogger(getClass());
	
	protected SysUserEntity getUser() {
		return (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
	}

	protected Long getUserId() {
		return getUser().getUserId();
	}

	protected List<Long> getRoleIdList() {
		List<Long> roles = getUser().getRoleIdList();
		if (roles == null) {
			roles = sysUserRoleService.queryRoleIdList(getUserId());
		}
		return roles;
	}

	protected boolean hasRole(Long roleId) {
		return getRoleIdList().contains(roleId);
	}
}
